package io.shuttle.mbe.api

import io.shuttle.mbe.api.annotation.ChromeMinVersion
import io.shuttle.mbe.api.annotation.PromiseStyleMinVersion
import io.shuttle.mbe.api.types.Value1Function
import io.shuttle.mbe.api.types.VoidFunction
import io.shuttle.mbe.core.Promise

////////////////////
// History
////////////////////
/**
 * Use the `Browser.history` API to interact with the browser's record of visited pages. You can add, remove, and query for URLs in the browser's history. To override the history page with your own version, see Override Pages.
 *
 * Permissions: "history"
 */
interface History {
    // Adds a URL to the history at the current time with a transition type of "link".
    @PromiseStyleMinVersion(96)
     fun addUrl(
        details: UrlDetails,
        callback: VoidFunction? = null
    ): Promise<Void>

    // Deletes all items from the history.
    @PromiseStyleMinVersion(96)
     fun deleteAll(
        callback: VoidFunction? = null
    ): Promise<Void>

    // Removes all items within the specified date range from the history.
    // Pages will not be removed from the history unless all visits fall within the range.
    @PromiseStyleMinVersion(96)
     fun deleteRange(
        range: Range,
        callback: VoidFunction? = null
    ): Promise<Void>

    // Removes all occurrences of the given URL from the history.
    @PromiseStyleMinVersion(96)
     fun deleteUrl(
        details: UrlDetails,
        callback: VoidFunction? = null
    ): Promise<Void>

    // Retrieves information about visits to a URL.
    @PromiseStyleMinVersion(96)
     fun getVisits(
        details: UrlDetails,
        callback: Value1Function<List<VisitItem>>? = null
    ): Promise<List<VisitItem>>

    // Searches the history for the last visit time of each page matching the query.
    @PromiseStyleMinVersion(96)
     fun search(
        query: SearchOptions,
        callback: Value1Function<List<HistoryItem>>? = null
    ): Promise<List<HistoryItem>>

    // Fired when a URL is visited, providing the HistoryItem data for that URL.
    // This event fires before the page has loaded.
     val onVisited: Events.Event<Value1Function<HistoryItem>>

    // Fired when one or more URLs are removed from history. When all visits have been removed the URL is purged from history.
     val onVisitRemoved: Events.Event<Value1Function<VisitRemovedDetails>>

    // An object encapsulating one result of a history query.
    data class HistoryItem(
        // The unique identifier for the item.
        var id: String,
        // When this page was last loaded, represented in milliseconds since the epoch.
        var lastVisitTime: Number?,
        // The title of the page when it was last loaded.
        var title: String?,
        // The number of times the user has navigated to this page by typing in the address.
        var typedCount: Number?,
        // The URL navigated to by a user.
        var url: String?,
        // The number of times the user has navigated to this page.
        var visitCount: Number?,
    )

    // The transition type for this visit from its referrer.
    @ChromeMinVersion(44)
    enum class TransitionType {
        // The user arrived at this page by clicking a link on another page.
        link,

        // The user arrived at this page by typing the URL in the address bar.
        // This is also used for other explicit navigation actions.
        typed,

        // The user arrived at this page through a suggestion in the UI, for example, through a menu item.
        auto_bookmark,

        // The user arrived at this page through subframe navigation that they didn't request,
        // such as through an ad loading in a frame on the previous page.
        // These don't always generate new navigation entries in the back and forward menus.
        auto_subframe,

        // The user arrived at this page by selecting something in a subframe.
        manual_subframe,

        // The user arrived at this page by typing in the address bar and selecting an entry that didn't look like a URL,
        // such as a Google Search suggestion.
        // For example, a match might have the URL of a Google Search result page,
        // but it might appear to the user as "Search Google for ...".
        // These are different from typed navigations because the user didn't type or see the destination URL.
        // They're also related to keyword navigations.
        generated,

        // The page was specified in the command line or is the start page.
        auto_toplevel,

        // The user arrived at this page by filling out values in a form and submitting the form.
        // Not all form submissions use this transition type.
        form_submit,

        // The user reloaded the page, either by clicking the reload button or by pressing Enter in the address bar.
        // Session restore and Reopen closed tab also use this transition type.
        reload,

        // The URL for this page was generated from a replaceable keyword other than the default search provider.
        keyword,

        // Corresponds to a visit generated for a keyword.
        keyword_generated
    }

    @ChromeMinVersion(88)
    data class UrlDetails(
        // The URL for the operation. It must be in the format as returned from a call to history.search().
        var url: String,
    )

    // An object encapsulating one visit to a URL.
    data class VisitItem(
        // The unique identifier for the corresponding history.HistoryItem.
        var id: String,
        // True if the visit originated on this device. False if it was synced from a different device.
        @ChromeMinVersion(115)
        var isLocal: Boolean,
        // The visit ID of the referrer.
        var referringVisitId: String,
        // The transition type for this visit from its referrer.
        var transition: TransitionType,
        // The unique identifier for this visit.
        var visitId: String,
        // When this visit occurred, represented in milliseconds since the epoch.
        var visitTime: Number?,
    )

    data class Range(
        // Items added to history before this date, represented in milliseconds since the epoch.
        var endTime: Number,
        // Items added to history after this date, represented in milliseconds since the epoch.
        var startTime: Number,
    )

    data class SearchOptions(
        // Limit results to those visited before this date, represented in milliseconds since the epoch.
        var endTime: Number,
        // The maximum number of results to retrieve. Defaults to 100.
        var maxResults: Number?,
        // Limit results to those visited after this date, represented in milliseconds since the epoch.
        // If property is not specified, it will default to 24 hours.
        var startTime: Number,
        // A free-text query to the history service. Leave this empty to retrieve all pages.
        var text: String,
    )

    data class VisitRemovedDetails(
        // True if all history was removed. If true, then urls will be empty.
        var allHistory: Boolean,
        var urls: List<String>?
    )
}